#combination generation courtesy of Uncle Bob:
#http://butunclebob.com/ArticleS.UncleBob.RubyCombinations
module Rical
  class Combination
    def self.c(n,r,&proc)
      if (n>0 && r>0)
        combine([], 0, n, r, proc)
      end
    end

    def self.get(n,r) 
      combinations = []
      self.c(n,r) {|c| combinations << c}
      combinations
    end

    private
    def self.combine(combination, s, n, r, proc)
      if (r == 0)
        proc.call(combination)
      else
        (s..(n-r)).each {|i| combine(combination + [i], i+1, n, r-1, proc)}
      end
    end
  end
end